import request from '.'

interface ApiResponse<T = unknown> {
  code: number
  data: T
  message: string
}

export const updateNewsItem = <T>(data: { id: number; [key: string]: unknown }) => {
  const token = JSON.parse(localStorage.getItem('login'))
  return request.post('/news/updateNews', data, {
    headers: {
      Authorization: 'Bearer ${token}',
    },
  }) as Promise<ApiResponse<T>>
}

export const getNewsList = <T>() => {
  return request.post('/news/releaseList') as Promise<ApiResponse<T>>
}

export const deleteNewsItem = <T>(data: { ids: number }) => {
  return request.post('/news/deleteNews', data) as Promise<ApiResponse<T>>
}

interface INews {
  id?: number
  newsTitle?: string
  title?: string
  abstractReal: string
  classReal?: number
  classModel?: string
  newsSource?: string
  newsTime?: string
  coverImage?: string
  isSelect?: number
  addMode: number
  siteName?: string
  opPerson?: string
  createTime?: string
  pubTime?: string
  isPublished?: number
  markContent?: string
  sourceUrl?: string
  pubState?: number
}

export const createNews = <T>(data: INews) => {
  return request.post('/news/insertNews', data) as Promise<ApiResponse<T>>
}

export const updateNews = <T>(data: Partial<INews> & { id: number }) => {
  return request.post('/news/updateNews', data) as Promise<ApiResponse<T>>
}

export const selectPublishNews = <T>(data: INews) => {
  return request.post('/news/selectPublish', data) as Promise<ApiResponse<T>>
}

export const orderNews = <T>(data: { id: number; orderNews: number }[]) => {
  return request.post('/news/order', { list: data }) as Promise<ApiResponse<T>>
}

export const newsIssueTemplate = <T>() => {
  return request.post('/news/newsIssueTemplate') as Promise<ApiResponse<T>>
}

interface NewsListParams {
  current: number
  currentSize: number
  issueNo: number
}

export const newsIssueList =<T> (data: NewsListParams) => {
  return request.post('/news/issueList', data)as Promise<ApiResponse<T>>
}

export const newsIssueNews = <T>(issueNo: number) => {
  return request.get(`/news/issueNews?issueNo=${issueNo}`) as Promise<ApiResponse<T>>
}